package org.javaboy.vhr.controller.employee;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.javaboy.vhr.model.*;
import org.javaboy.vhr.service.*;
import org.javaboy.vhr.responseBean.ServerResponse;
import org.javaboy.vhr.util.POIUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 员工基础信息controller
 */
@RestController
@RequestMapping("employee/basic")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsstatusService politicsstatusService;
    @Autowired
    PositionService positionService;

    /**
     * 分页查询员工数据
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/")
    public ServerResponse getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, Employee employee, Date[] beginWorkDate){
        PageBean pageBean = employeeService.getEmployeeByPage(page,size,employee,beginWorkDate);
        return ServerResponse.success("",pageBean);
    }

    /**
     * 添加员工信息
     * @param employee
     * @return
     */
    @PostMapping("/")
    public ServerResponse addEmployee(@RequestBody Employee employee){
        if(employeeService.addEmployee(employee)==1){
            return ServerResponse.success("添加成功");
        }
        return ServerResponse.error(1000,"添加失败");
    }

    /**
     * 删除员工信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ServerResponse deleteEmployeeById(@PathVariable Integer id){
        int result = employeeService.deleteEmployeeById(id);
        if(result==1){
            return ServerResponse.success("删除成功");
        }
        return ServerResponse.error(1001,"删除失败");
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    @PutMapping("/")
    public  ServerResponse updateEmployee(@RequestBody Employee employee){
        int result = employeeService.updateEmployee(employee);
        if(result==1){
            return ServerResponse.success("修改成功");
        }
        return ServerResponse.error(1000,"修改失败");
    }

    /**
     * 获取政治面貌
     * @return
     */
    @GetMapping("/politicsstatusList")
    public ServerResponse getAllPoliticsstatus(){
        List<Politicsstatus> list = politicsstatusService.getAllPoliticsstatus();
        return ServerResponse.success("",list);
    }

    /**
     * 获取民族数据
     * @return
     */
    @GetMapping("/nationList")
    public ServerResponse getAllNation(){
        List<Nation> list = nationService.getAllNation();
        return ServerResponse.success("",list);
    }

    /**
     * 获取职称列表
     * @return
     */
    @GetMapping("/jobLevelList")
    public ServerResponse getAllJobLevelList(){
        List<JobLevel> list = jobLevelService.getAllList();
        return ServerResponse.success("",list);
    }

    /**
     * 获取职位列表
     * @return
     */
    @GetMapping("/positionList")
    public ServerResponse getAllPositionList(){
        List<Position> list = positionService.getAllPosition();
        return ServerResponse.success("",list);
    }

    /**
     * 获取最大工号
     * @return
     */
    @GetMapping("/getMaxWorkId")
    public ServerResponse getMaxWorkId(){
        String workId = String.format("%08d", employeeService.getMaxWorkId() + 1);
        return ServerResponse.success("",workId);
    }


    /**
     * 导出数据
     * @return
     */
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData(){
        // 获取员工信息
        PageBean employeeByPage = employeeService.getEmployeeByPage(null, null, null,null);
        List<Employee> list = (List<Employee>) employeeByPage.getList();
        // 使用poi生成Excel

        // 导出excel数据
        return POIUtil.employee2Excel(list);
    }

    /**
     * 导入数据
     * @return
     */
    @PostMapping("/import")
    public ServerResponse importData(MultipartFile file) throws IOException {
        file.transferTo(new File("C:\\Users\\Administrator\\Desktop\\1111111.xls"));
        // 解析excel文件
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        workbook.getSheetAt(0);
        return ServerResponse.success("上传成功");
    }
}
